package com.yanqu.road.server.cmd.orewar.cross;

import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.battle.BattleManager;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_ORE_WAR_DETECT_USER, desc = "跨服返回侦查玩家门客信息")
public class CrossOreDetectUserPitMsgCmd extends AbstractCommand {

	private int activityId;

	private int pitId;

	private int patronsCount;

	private BigInteger ability;

	private List<UserPatrons> patronsList;

	private CrossUserOrePit crossUserOrePit;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.CrossOreDetectUserOrePitRespMsg crossSyncMsg = OreWarProto.CrossOreDetectUserOrePitRespMsg.parseFrom(packet.getBytes());
		ret = crossSyncMsg.getRet();
		if(ret == 0){
			crossUserOrePit =  OreWarPb.parseCrossUserOrePit(crossSyncMsg.getOrePit());
		}
		if(null == crossUserOrePit){
			ret = GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND;
			return;
		}
		if(crossUserOrePit.getUserId() != crossSyncMsg.getUserId()){
			ret = GameErrorCode.E_ORE_WAR_ORE_PIT_OWNER_CHANGE;
			return;
		}
		//判断是否足够侦查道具
		if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(GameConfig.MINE_WAR_DETECT_CONSUME),
				eLogMoneyType.OreWar, eLogMoneyType.OreWarDetect)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}

		patronsCount = crossUserOrePit.getPatronsList().size();

		patronsList = BattleManager.getCalOreWarEarnSpeed(crossUserOrePit.getPatronsList());

		//获取总实力
		ability = PatronsMgr.getOreWarPatronsListEarnSpeed(patronsList);
	}

	@Override
	public void sendData(GamePlayer player) {
		OreWarProto.OreDetectUserOrePitRespMsg.Builder respMsg = OreWarProto.OreDetectUserOrePitRespMsg.newBuilder();
		if(0 == ret){
			respMsg.setPatronsCount(patronsCount);
			respMsg.setTotalAbility(ability.longValue());
			for (UserPatrons userPatrons : patronsList) {
				PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
				if (null != patronsMsg) {
					respMsg.addPatronsList(patronsMsg);
				}
			}
			respMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(crossUserOrePit.getUserBaseInfo()));
		}
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_ORE_WAR_DETECT_USER_PIT, respMsg);
	}


}
